#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :算数品牌榜单.py
# @Time      :2024/11/27 
# @Author    :CL
# @email     :1037654919@qq.com
import datetime

import pandas as pd
import requests
from tqdm import tqdm
USER_AGENT_LIST = [
    'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36',
    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36',
    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36',
    'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6',
    'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36',
    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36',
    'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0'
]
from for_AES import decrtptlx

branddict={'汽车':1,

'手机':2,

'美妆':3,

'奢饰品':4,

'食品饮料':5,

'家用电器':6,

'服饰鞋帽':7,

'母婴':9,

'日化':10}
# from scrapy_juliangsuanshu import *
#  算数品牌榜单 必须以周六作为起始日
def get_brand_index_weekly(datestart,dateend,branddict):
    print('begin function:get_brand_index_weekly')

    for brand_category,category_id in tqdm(branddict.items()):
        print('begin %s' %brand_category)
        while datestart <= dateend:   #周循环
            print('当前进度：', datestart, end=' ')
            #将20241112 格式日期往后加7天 datestart

            # 将字符串转换为 datetime 对象
            date_start = datetime.datetime.strptime(datestart, "%Y%m%d")

            # 增加7天
            date_end = date_start + datetime.timedelta(days=7)

            # 将结果转换回字符串
            datestart = date_end.strftime("%Y%m%d")
            dataxy = pd.DataFrame()
            try:
                sign_url = 'https://trendinsight.oceanengine.com/api/open/brand/get_brand_index_weekly?category_id=%s&app_name=aweme&num=50&date=%s' % (
                    category_id, datestart)

                headers2 = {"accept": "application/json, text/plain, */*", "accept-language": "zh-CN,zh;q=0.9",
                            "cache-control": "no-cache", "content-type": "application/json;charset=UTF-8",
                            "origin": "https://trendinsight.oceanengine.com", "pragma": "no-cache",
                            "priority": "u=1, i", "referer": "https://trendinsight.oceanengine.com/",
                            "sec-ch-ua": "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
                            "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"", "sec-fetch-dest": "empty",
                            "sec-fetch-mode": "cors", "sec-fetch-site": "cross-site",
                            "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
                            "x-feelgood-api-version": "v2", "x-fg-report-id": "cdb41390-8421-4ef8-ab04-b1e738ce900b",
                            "x-fg-tea-id": "7351613029732238883%2C7351613025344325155%2C7351613030817334836%2C7351613017912903203",
                            }
                datastr = requests.get(sign_url, headers=headers2).json()['data']
                requests.session().close()
                data = decrtptlx(datastr)
                data0 = data.lstrip('[').rstrip(']')
                data1 = eval(data0)
                brandData = data1['brandData']
                for value in brandData:
                    dataxy = dataxy.append(
                        [{'brand_category':brand_category,'category_id':category_id,'date': datestart, 'brand_id': value['brand_id'], 'brand_name': value['brand_name'],
                          'index': value['index'], 'rank_change': value['rank_change'],
                          'rate_change': value['rate_change'], 'rank': value['rank'], 'app_name': 'aweme'}],
                        ignore_index=True)
                yield dataxy
            except:
                print("%s %s fialed" % (brand_category, datestart))

if __name__ == '__main__':
    for data in get_brand_index_weekly(datestart ='20241116',dateend='20241123',branddict=branddict):
        print(data)
        break